Consider we have a full infinite set at first Use a running curr to keep track of smallest number and priority queue to keep track if we have scarlet numbers before running smallest
class SmallestInfiniteSet {
PriorityQueue<Integer> pq = new PriorityQueue<>();
int curr;
public SmallestInfiniteSet() {
// set curr smallest to 1
curr = 1;
}
public int popSmallest() {
if(!pq.isEmpty())return pq.poll();
// if we have nothing in pq, curr smallest is what we poll out, and sus next number would be curr + 1
curr ++;
return curr - 1;
}
public void addBack(int num) {
if(num < curr & !pq.contains(num)){
// number is small and we dont have it in our pq
// add back
pq.add(num);
}
}
}
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
* int param_1 = obj.popSmallest();
* obj.addBack(num);
*/
